package com.yh.threadTest.pojo;

import com.yh.threadTest.utils.WebDownloader;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @ClassName MyCallable
 * @Description TODO
 * @Author 0715-YuHao
 * @Date 2020/11/14 11:50
 */
public class MyCallable implements Callable<Boolean> {

    private String url;
    private String name;

    public MyCallable(String url, String name) {
        this.url = url;
        this.name = name;
    }

    @Override
    public Boolean call() throws Exception {
        WebDownloader.downloader(url, name);
        System.out.println("下载了" + name);
        return true;
    }

    public static void main(String[] args) {
        FutureTask<Boolean> futureTask = new FutureTask<>(new MyCallable("https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=2075903360,405209795&fm=26&gp=0.jpg", "1.jpg"));
        FutureTask<Boolean> futureTask2 = new FutureTask<>(new MyCallable("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1605336680997&di=63ffd3a5d5334b3871fc1d46b086e378&imgtype=0&src=http%3A%2F%2Fattach.bbs.miui.com%2Fforum%2F201308%2F23%2F220651x9b0h4kru904ozre.jpg", "2.jpg"));
        FutureTask<Boolean> futureTask3 = new FutureTask<>(new MyCallable("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1605336680996&di=3932240817f9df0726ffb5df13bbd1e3&imgtype=0&src=http%3A%2F%2Fc.hiphotos.baidu.com%2Fzhidao%2Fpic%2Fitem%2F8435e5dde71190efa4261f43ce1b9d16fcfa6049.jpg", "3.jpg"));
        new Thread(futureTask).start();
        new Thread(futureTask2).start();
        new Thread(futureTask3).start();
        try {
            Boolean b1 = futureTask.get();
            Boolean b2 = futureTask.get();
            Boolean b3 = futureTask.get();
            System.out.println(b1);
            System.out.println(b2);
            System.out.println(b3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
}
